<?php 
class ControllerProductProduct extends Controller {
	private $error = array(); 
     
  	public function index() {
		$this->load->language('product/product');
		$this->document->setTitle($this->language->get('heading_title')); 
		$this->load->model('product/product');
		$this->getList();
  	}
  
  	public function insert() {
    	$this->load->language('product/product');
    	$this->document->setTitle($this->language->get('heading_title')); 
		$this->load->model('product/product');
    	if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
			$this->model_product_product->addProduct($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('product/product', 'token=' . $this->session->data['token'], 'SSL'));
    	}
    	$this->getForm_text();
    	$this->getForm_insert();
  	}

  	public function update() {
    	$this->load->language('product/product');
    	$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/product');
    	if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
			$this->model_product_product->editProduct($this->request->get['product_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('product/product', 'token=' . $this->session->data['token'], 'SSL'));
		}
    	$this->getForm_text();
    	$this->getForm_update();
  	}

  	private function getList() {
  		$this->data['heading_title'] = $this->language->get('heading_title');		
				
		$this->data['text_enabled'] = $this->language->get('text_enabled');		
		$this->data['text_disabled'] = $this->language->get('text_disabled');		
		$this->data['text_no_results'] = $this->language->get('text_no_results');		
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');		
		
		$this->data['column_image'] = $this->language->get('column_image');
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_display_name'] = $this->language->get('column_display_name');
		$this->data['column_ean'] = $this->language->get('column_ean');
		$this->data['column_model'] = $this->language->get('column_model');
		$this->data['column_price'] = $this->language->get('column_price');
		$this->data['column_quantity'] = $this->language->get('column_quantity');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
		$this->data['column_action'] = $this->language->get('column_action');
				
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_filter'] = $this->language->get('button_filter');
		 
 		$this->data['token'] = $this->session->data['token'];
 		
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/product', 'token=' . $this->session->data['token'], 'SSL'),       		
      		'separator' => ' :: '
   		);
		$this->data['insert'] = $this->url->link('product/product/insert', 'token=' . $this->session->data['token'], 'SSL');
					
		if (isset($this->request->get['filter_name'])) {
			$filter_name = $this->request->get['filter_name'];
		} else {
			$filter_name = null;
		}
  		if (isset($this->request->get['filter_ean'])) {
			$filter_ean = $this->request->get['filter_ean'];
		} else {
			$filter_ean = null;
		}
		if (isset($this->request->get['filter_model'])) {
			$filter_model = $this->request->get['filter_model'];
		} else {
			$filter_model = null;
		}
		if (isset($this->request->get['filter_price_min'])) {
			$filter_price_min = $this->request->get['filter_price_min'];
		} else {
			$filter_price_min = null;
		}
		if (isset($this->request->get['filter_price_max'])) {
			$filter_price_max = $this->request->get['filter_price_max'];
		} else {
			$filter_price_max = null;
		}
		if (isset($this->request->get['filter_quantity_min'])) {
			$filter_quantity_min = $this->request->get['filter_quantity_min'];
		} else {
			$filter_quantity_min = null;
		}
		if (isset($this->request->get['filter_quantity_max'])) {
			$filter_quantity_max = $this->request->get['filter_quantity_max'];
		} else {
			$filter_quantity_max = null;
		}
		if (isset($this->request->get['filter_category_id'])) {
			$filter_category_id = $this->request->get['filter_category_id'];
		} else {
			$filter_category_id = null;
		}
		if (isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
		} else {
			$filter_status = -1;
		}
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'pd.display_name';
		}
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
						
		$data = array(
			'filter_name'	      => $filter_name,
			'filter_ean'	      => $filter_ean,
			'filter_model'	      => $filter_model,
			'filter_price_min'    => $filter_price_min,
			'filter_price_max'    => $filter_price_max,
			'filter_quantity_min' => $filter_quantity_min,
			'filter_quantity_max' => $filter_quantity_max,
			'filter_category_id'  => $filter_category_id,
			'filter_status'       => $filter_status,
			'sort'                => $sort,
			'order'               => $order,
			'start'               => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'               => $this->config->get('config_admin_limit')
		);
		$product_total = $this->model_product_product->getTotalProducts($data);
		$results = $this->model_product_product->getProducts($data);
		$this->load->model('tool/image');
		$this->data['products'] = array();
		foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('product/product/update', 'token=' . $this->session->data['token'] . '&product_id=' . $result['product_id'], 'SSL')
			);
			
			if ($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
				$image = $this->model_tool_image->resize($result['image'], 40, 40);
			} else {
				$image = $this->model_tool_image->resize('no_image.jpg', 40, 40);
			}
	
      		$this->data['products'][] = array(
				'product_id' => $result['product_id'],
				'name'       => $result['name'],
				'ean'        => $result['ean'],
				'model'      => $result['model'],
				'image'      => $image,
				'price'      => $result['price'],
				'quantity'   => $result['quantity'],
				'sort_order' => $result['sort_order'],
				'status'     => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'display_name' => $result['display_name'],
				'action'     => $action
			);
    	}
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';
		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}
  		if (isset($this->request->get['filter_ean'])) {
			$url .= '&filter_ean=' . urlencode(html_entity_decode($this->request->get['filter_ean'], ENT_QUOTES, 'UTF-8'));
		}
		if (isset($this->request->get['filter_model'])) {
			$url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));
		}
  		if (isset($this->request->get['filter_price_min'])) {
			$url .= '&filter_price_min=' . $this->request->get['filter_price_min'];
		}
  		if (isset($this->request->get['filter_price_max'])) {
			$url .= '&filter_price_max=' . $this->request->get['filter_price_max'];
		}
  		if (isset($this->request->get['filter_quantity_min'])) {
			$url .= '&filter_quantity_min=' . $this->request->get['filter_quantity_min'];
		}
  		if (isset($this->request->get['filter_quantity_max'])) {
			$url .= '&filter_quantity_max=' . $this->request->get['filter_quantity_max'];
		}
  		if (isset($this->request->get['filter_category_id'])) {
			$url .= '&filter_category_id=' . $this->request->get['filter_category_id'];
		}
		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}
		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
					
		$this->data['sort_name'] = $this->url->link('product/product', 'token=' . $this->session->data['token'] . '&sort=pd.display_name' . $url, 'SSL');
		$this->data['sort_ean'] = $this->url->link('product/product', 'token=' . $this->session->data['token'] . '&sort=p.ean' . $url, 'SSL');
		$this->data['sort_model'] = $this->url->link('product/product', 'token=' . $this->session->data['token'] . '&sort=p.model' . $url, 'SSL');
		$this->data['sort_price'] = $this->url->link('product/product', 'token=' . $this->session->data['token'] . '&sort=p.price' . $url, 'SSL');
		$this->data['sort_quantity'] = $this->url->link('product/product', 'token=' . $this->session->data['token'] . '&sort=p.quantity' . $url, 'SSL');
		$this->data['sort_status'] = $this->url->link('product/product', 'token=' . $this->session->data['token'] . '&sort=p.status' . $url, 'SSL');
		$this->data['sort_sort_order'] = $this->url->link('product/product', 'token=' . $this->session->data['token'] . '&sort=p.sort_order' . $url, 'SSL');
		
		$url = '';
  		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . urlencode(html_entity_decode($this->request->get['filter_name'], ENT_QUOTES, 'UTF-8'));
		}
  		if (isset($this->request->get['filter_ean'])) {
			$url .= '&filter_ean=' . urlencode(html_entity_decode($this->request->get['filter_ean'], ENT_QUOTES, 'UTF-8'));
		}
		if (isset($this->request->get['filter_model'])) {
			$url .= '&filter_model=' . urlencode(html_entity_decode($this->request->get['filter_model'], ENT_QUOTES, 'UTF-8'));
		}
  		if (isset($this->request->get['filter_price_min'])) {
			$url .= '&filter_price_min=' . $this->request->get['filter_price_min'];
		}
  		if (isset($this->request->get['filter_price_max'])) {
			$url .= '&filter_price_max=' . $this->request->get['filter_price_max'];
		}
  		if (isset($this->request->get['filter_quantity_min'])) {
			$url .= '&filter_quantity_min=' . $this->request->get['filter_quantity_min'];
		}
  		if (isset($this->request->get['filter_quantity_max'])) {
			$url .= '&filter_quantity_max=' . $this->request->get['filter_quantity_max'];
		}
  		if (isset($this->request->get['filter_category_id'])) {
			$url .= '&filter_category_id=' . $this->request->get['filter_category_id'];
		}
		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
				
		$pagination = new Pagination();
		$pagination->total = $product_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('product/product', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();
	
		$this->data['filter_name'] = $filter_name;
		$this->data['filter_ean'] = $filter_ean;
		$this->data['filter_model'] = $filter_model;
		$this->data['filter_price_min'] = $filter_price_min;
		$this->data['filter_price_max'] = $filter_price_max;
		$this->data['filter_quantity_min'] = $filter_quantity_min;
		$this->data['filter_quantity_max'] = $filter_quantity_max;
		$this->data['filter_category_id'] = $filter_category_id;
		$this->data['filter_status'] = $filter_status;
		
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'product/product_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
  	}

  	private function getForm_text() {
    	$this->data['heading_title'] = $this->language->get('heading_title');
 
    	$this->data['text_enabled'] = $this->language->get('text_enabled');
    	$this->data['text_disabled'] = $this->language->get('text_disabled');
    	$this->data['text_none'] = $this->language->get('text_none');
    	$this->data['text_yes'] = $this->language->get('text_yes');
    	$this->data['text_no'] = $this->language->get('text_no');
		$this->data['text_default'] = $this->language->get('text_default');
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');
		$this->data['text_browse'] = $this->language->get('text_browse');
		$this->data['text_clear'] = $this->language->get('text_clear');
		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_related_search'] = $this->language->get('text_related_search');
		
		$this->data['column_display_name'] = $this->language->get('column_display_name');
		$this->data['column_price'] = $this->language->get('column_price');
		
    	$this->data['entry_category'] = $this->language->get('entry_category');
		$this->data['entry_manufacturer'] = $this->language->get('entry_manufacturer');
		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_ean'] = $this->language->get('entry_ean');
    	$this->data['entry_model'] = $this->language->get('entry_model');
    	$this->data['entry_image'] = $this->language->get('entry_image');
    	$this->data['entry_price'] = $this->language->get('entry_price');
    	$this->data['entry_weight'] = $this->language->get('entry_weight');
    	$this->data['entry_quantity'] = $this->language->get('entry_quantity');
		$this->data['entry_minimum'] = $this->language->get('entry_minimum');
		$this->data['entry_processing_day'] = $this->language->get('entry_processing_day');
		$this->data['entry_layout'] = $this->language->get('entry_layout');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_status'] = $this->language->get('entry_status');
    	
		$this->data['entry_display_name'] = $this->language->get('entry_display_name');
		$this->data['entry_meta_keyword'] = $this->language->get('entry_meta_keyword');
		$this->data['entry_meta_description'] = $this->language->get('entry_meta_description');
		$this->data['entry_tag'] = $this->language->get('entry_tag');
		$this->data['entry_description'] = $this->language->get('entry_description');
		
		$this->data['entry_category_add'] = $this->language->get('entry_category_add');
		$this->data['entry_related'] = $this->language->get('entry_related');
		
		$this->data['entry_attr_display_name'] = $this->language->get('entry_attr_display_name');
		$this->data['entry_attr_sort'] = $this->language->get('entry_attr_sort');
		$this->data['entry_attr_group_name'] = $this->language->get('entry_attr_group_name');
		$this->data['entry_attr_group_display_name'] = $this->language->get('entry_attr_group_display_name');
		$this->data['entry_attr_value_name'] = $this->language->get('entry_attr_value_name');
		$this->data['entry_attr_value_display_name'] = $this->language->get('entry_attr_value_display_name');
		$this->data['entry_attr_value_sort'] = $this->language->get('entry_attr_value_sort');
		
		$this->data['entry_required'] = $this->language->get('entry_required');
		$this->data['entry_option_display_name'] = $this->language->get('entry_option_display_name');
		$this->data['entry_option_type'] = $this->language->get('entry_option_type');
		$this->data['entry_option_sort'] = $this->language->get('entry_option_sort');
		$this->data['entry_option_value_name'] = $this->language->get('entry_option_value_name');
		$this->data['entry_option_value_display_name'] = $this->language->get('entry_option_value_display_name');
		$this->data['entry_option_value_quantity'] = $this->language->get('entry_option_value_quantity');
		$this->data['entry_option_value_price'] = $this->language->get('entry_option_value_price');
		$this->data['entry_option_value_weight'] = $this->language->get('entry_option_value_weight');
		$this->data['entry_option_value_sort'] = $this->language->get('entry_option_value_sort');
		
		$this->data['entry_wholesale_min_num'] = $this->language->get('entry_wholesale_min_num');
		$this->data['entry_wholesale_max_num'] = $this->language->get('entry_wholesale_max_num');
		$this->data['entry_wholesale_price'] = $this->language->get('entry_wholesale_price');
		
		$this->data['entry_promotion_name'] = $this->language->get('entry_promotion_name');
		$this->data['entry_promotion_display_name'] = $this->language->get('entry_promotion_display_name');
		$this->data['entry_promotion_status'] = $this->language->get('entry_promotion_status');
		$this->data['entry_promotion_start_time'] = $this->language->get('entry_promotion_start_time');
		$this->data['entry_promotion_end_time'] = $this->language->get('entry_promotion_end_time');
		$this->data['entry_product_promotion_price'] = $this->language->get('entry_product_promotion_price');
		$this->data['entry_product_promotion_quantity'] = $this->language->get('entry_product_promotion_quantity');
		$this->data['entry_product_promotion_start_time'] = $this->language->get('entry_product_promotion_start_time');
		$this->data['entry_product_promotion_end_time'] = $this->language->get('entry_product_promotion_end_time');
		
				
    	$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_add_attribute'] = $this->language->get('button_add_attribute');
		$this->data['button_add_attr_value'] = $this->language->get('button_add_attr_value');
		$this->data['button_add_option'] = $this->language->get('button_add_option');
		$this->data['button_add_option_value'] = $this->language->get('button_add_option_value');
		$this->data['button_add_wholesale'] = $this->language->get('button_add_wholesale');
		$this->data['button_add_promotion'] = $this->language->get('button_add_promotion');
		$this->data['button_add_image'] = $this->language->get('button_add_image');
		$this->data['button_remove'] = $this->language->get('button_remove');
		
    	$this->data['tab_data'] = $this->language->get('tab_data');
    	$this->data['tab_general'] = $this->language->get('tab_general');	
		$this->data['tab_links'] = $this->language->get('tab_links');
		$this->data['tab_attribute'] = $this->language->get('tab_attribute');
		$this->data['tab_option'] = $this->language->get('tab_option');		
		$this->data['tab_wholesale'] = $this->language->get('tab_wholesale');
		$this->data['tab_promotion'] = $this->language->get('tab_promotion');
    	$this->data['tab_image'] = $this->language->get('tab_image');
		 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/product', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);

		$this->data['token'] = $this->session->data['token'];
		
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
				
		$this->load->model('product/manufacturer');
    	$this->data['manufacturers'] = $this->model_product_manufacturer->getManufacturers();

		$this->load->model('sale/promotion');
		$this->data['promotions'] = $this->model_sale_promotion->getPromotionsToSelect();
		
		$this->load->model('product/category');
		$this->data['categories'] = $this->model_product_category->getAllCategoriesForSelect(0, '', '', -1, 0);
		
		$this->load->model('design/layout');
		$this->data['layouts'] = $this->model_design_layout->getLayouts();
  	}
  	
	private function getForm_insert() {
		$this->data['action'] = $this->url->link('product/product/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('product/product', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['product_id'] = '';
		$this->data['category_id'] = '';
		$this->data['manufacturer_id'] = 0;
		$this->data['name'] = '';
		$this->data['ean'] = '';
		$this->data['model'] = '';
		$this->data['image'] = '';
		$this->data['price'] = '';
		$this->data['weight'] = '';
		$this->data['quantity'] = 100;
		$this->data['minimum'] = 1;
		$this->data['processing_day'] = 1;
		$this->data['layout_id'] = 0;
		$this->data['sort_order'] = 999;
		$this->data['status'] = 0;
		
		$this->data['product_description'] = array();
		$this->data['product_category'] = array();
		$this->data['product_related'] = array();
		$this->data['product_attributes'] = array();
		$this->data['product_options'] = array();
		$this->data['product_wholesales'] = array();
		$this->data['product_promotions'] = array();
		$this->data['product_images'] = array();

		$this->load->model('tool/image');
		$this->data['thumb'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);

		$this->template = 'product/product_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
  	}
  	
	private function getForm_update() {
		$this->data['action'] = $this->url->link('product/product/update', 'token=' . $this->session->data['token'] . '&product_id=' . $this->request->get['product_id'], 'SSL');
		$this->data['cancel'] = $this->url->link('product/product', 'token=' . $this->session->data['token'], 'SSL');

		$product_info = $this->model_product_product->getProduct($this->request->get['product_id']);
    	$this->data['product_id'] = $product_info['product_id'];
		$this->data['category_id'] = $product_info['category_id'];
		$this->data['manufacturer_id'] = $product_info['manufacturer_id'];
		$this->data['name'] = $product_info['name'];
		$this->data['ean'] = $product_info['ean'];
		$this->data['model'] = $product_info['model'];
		$this->data['image'] = $product_info['image'];
		$this->data['price'] = $product_info['price'];
		$this->data['weight'] = $product_info['weight'];
		$this->data['quantity'] = $product_info['quantity'];
		$this->data['minimum'] = $product_info['minimum'];
		$this->data['processing_day'] = $product_info['processing_day'];
		$this->data['layout_id'] = $product_info['layout_id'];
		$this->data['sort_order'] = $product_info['sort_order'];
		$this->data['status'] = $product_info['status'];
		$this->data['date_added'] = $product_info['date_added'];
		$this->data['date_modified'] = $product_info['date_modified'];

		$this->load->model('tool/image');
		if ($product_info['image'] && file_exists(DIR_IMAGE . $product_info['image'])) {
			$this->data['thumb'] = $this->model_tool_image->resize($product_info['image'], 100, 100);
		} else {
			$this->data['thumb'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		}
		
		$this->data['product_description'] = $this->model_product_product->getProductDescriptions($this->request->get['product_id']);
		$this->data['product_category'] = $this->model_product_product->getProductCategories($this->request->get['product_id']);
		$this->data['product_related'] = $this->model_product_product->getProductRelated($this->request->get['product_id']);
		$this->data['product_attributes'] = $this->model_product_product->getProductAttributes($this->request->get['product_id']);
		$this->data['product_options'] = $this->model_product_product->getProductOptions($this->request->get['product_id']);
		$this->data['product_wholesales'] = $this->model_product_product->getProductWholesale($this->request->get['product_id']);
		$this->data['product_promotions'] = $this->model_product_product->getProductPromotion($this->request->get['product_id']);
		
		$product_images = $this->model_product_product->getProductImages($this->request->get['product_id']);
		$this->data['product_images'] = array();
		foreach ($product_images as $product_image) {
			if ($product_image['image'] && file_exists(DIR_IMAGE . $product_image['image'])) {
				$image = $product_image['image'];
			} else {
				$image = 'no_image.jpg';
			}
			$this->data['product_images'][] = array(
				'image'      => $image,
				'thumb'      => $this->model_tool_image->resize($image, 100, 100),
				'sort_order' => $product_image['sort_order']
			);
		}
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
										
		$this->template = 'product/product_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
  	}
	
  	public function autocompleteRelated() {
		$json = array();
		if (isset($this->request->get['filter_product_id'])) {
			$this->load->model('product/product');
			$filter_product_id = $this->request->get['filter_product_id'];
			$results = $this->model_product_product->getProductForSelect($filter_product_id);
			
			foreach ($results as $result) {
				$json[] = array(
					'product_id'    => $result['product_id'],
					'display_name'  => html_entity_decode($result['display_name'], ENT_QUOTES, 'UTF-8'),	
					'price'         => $result['price']
				);	
			}
		}
		$this->response->setOutput(json_encode($json));
	}
	
	public function autocomplete() {
		$json = array();
		if (isset($this->request->get['filter_product_id'])) {
			$this->load->model('product/product');
			$filter_product_id = $this->request->get['filter_product_id'];
			$results = $this->model_product_product->getProductForSelect($filter_product_id);
			
			foreach ($results as $result) {
				$option_data = array();
				
				$product_options = $this->model_product_product->getProductOptions($result['product_id']);	
				
				foreach ($product_options as $product_option) {
					if ($product_option['type'] == 'select' || $product_option['type'] == 'radio' || $product_option['type'] == 'checkbox' || $product_option['type'] == 'image') {
						$option_value_data = array();
					
						foreach ($product_option['product_option_value'] as $product_option_value) {
							$option_value_data[] = array(
								'product_option_value_id' => $product_option_value['product_option_value_id'],
								'option_value_id'         => $product_option_value['option_value_id'],
								'name'                    => $product_option_value['name'],
								'price'                   => (float)$product_option_value['price'] ? $this->currency->format($product_option_value['price'], $this->config->get('config_currency')) : false,
								'price_prefix'            => $product_option_value['price_prefix']
							);	
						}
					
						$option_data[] = array(
							'product_option_id' => $product_option['product_option_id'],
							'option_id'         => $product_option['option_id'],
							'name'              => $product_option['name'],
							'type'              => $product_option['type'],
							'option_value'      => $option_value_data,
							'required'          => $product_option['required']
						);	
					} else {
						$option_data[] = array(
							'product_option_id' => $product_option['product_option_id'],
							'option_id'         => $product_option['option_id'],
							'name'              => $product_option['name'],
							'type'              => $product_option['type'],
							'option_value'      => $product_option['option_value'],
							'required'          => $product_option['required']
						);				
					}
				}
					
				$json[] = array(
					'product_id' => $result['product_id'],
					'name'       => html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'),	
					'model'      => $result['model'],
					'option'     => $option_data,
					'price'      => $result['price']
				);	
			}
		}

		$this->response->setOutput(json_encode($json));
	}
}
?>